<?php

class Application_Model_Pedido extends Zend_Db_Table_Abstract {

    protected $_name = 'pedidos';
    protected $_primary = 'idpedido';
    private static $instance;
    protected $_schema = 'pub';

    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new Application_Model_Pedido();
            return self::$instance;
        }
        return self::$instance;
    }

    public function insert($data) {
        if (isset($data['idreservas']) && isset($data['pago'])){
            return parent::insert($data);
        }
    }

    public function update(array $data, $where = '') {
        $where = 'idpedido = ' . $data['idpedido'];
        parent::update($data, $where);
    }

    public function getById($id) {
        return $this->fetchRow("idpedido = " . $id);
    }

    public function getAll(){
        $sql = 'select * from '.$this->_schema.'.'.$this->_name;
        return $this->getAdapter()->fetchAll($sql);
    }

    public function getList(){
        $sql = 'select idpedido,lugares from '.$this->_schema.'.'.$this->_name;
        return $this->getAdapter()->fetchAll($sql);
    }

    public function getPedidosByReserva($data,$mesa){
        $sql = "select p.* from pedidos p
                left join reservas r on r.idreservas = p.idreservas
                where r.data = '$data'
                and r.mesa = '$mesa'
                and r.hora_saida is null;";
        return $this->getAdapter()->fetchAll($sql);
    }
    
    public function remove($id){
        $where = ' idpedido = ' .$id;
        parent::delete($where);
    }
    
    
}